﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace BSPortal.Usercontrol.Admin.Book.Order
{
    public partial class ListOrder : System.Web.UI.UserControl
    {
        int _pageIndex = 1;
        MainService service = new MainService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rdFromDate.SelectedDate = DateTime.Today.AddDays(-3);
                rdToDate.SelectedDate = DateTime.Today;
                LoadCity();
                LoadDistrict();
                LoadWard();
                LoadData();
            }
        }

        protected void LoadCity()
        {
            var mainService = Common.GetClientService();
            var listCity = mainService.GetAllCity().OrderBy(row => row.Name);            

            ddlCity.DataValueField = "CityId";
            ddlCity.DataTextField = "Name";
            ddlCity.DataSource = listCity;
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("Tất cả", "-1"));
        }

        protected void LoadDistrict()
        {
            if (ddlCity.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listDistrict = mainService.GetAllDistrict(int.Parse(ddlCity.SelectedValue)).OrderBy(row => row.Name);

                ddlDistrict.DataValueField = "DistrictId";
                ddlDistrict.DataTextField = "Name";
                ddlDistrict.DataSource = listDistrict;
                ddlDistrict.DataBind();

                ddlDistrict.Items.Insert(0, new ListItem("Tất cả", "-1"));
            }
        }

        protected void LoadWard()
        {
            if (ddlDistrict.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listWard =
                    mainService.GetAllWard(int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue)).OrderBy(row => row.Name);

                ddlWard.DataValueField = "WardId";
                ddlWard.DataTextField = "Name";
                ddlWard.DataSource = listWard;
                ddlWard.DataBind();

                ddlWard.Items.Insert(0, new ListItem("Tất cả", "-1"));
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDistrict();
            ddlDistrict_SelectedIndexChanged(sender, e);
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadWard();
        }

        #region Load Data
        void LoadData()
        {
            BindData();
            
        }
        void LoadCategory()
        {
            List<CategorySearch> listCategorySearch = new List<CategorySearch>();

        }
        #endregion

        #region Grid Event
        protected void BindData()
        {
            try
            {
                int city, district, ward=-1;
                int.TryParse(ddlCity.SelectedValue, out city);
                int.TryParse(ddlDistrict.SelectedValue, out district);
                int.TryParse(ddlWard.SelectedValue, out ward);

                var data = service.GetOrderByConditions((DateTime)rdFromDate.SelectedDate, (DateTime)rdToDate.SelectedDate, txtKey.Text, txtKey.Text, txtKey.Text, txtKey.Text, Int32.Parse(ddlStatus.SelectedValue), city, district, ward);
                RadGrid.DataSource = data;
                RadGrid.VirtualItemCount = data.Count;
                RadGrid.PageSize = PageConst.PAGES_SIZE;
                RadGrid.DataBind();

            }
            catch (Exception exception)
            {

            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            int city = -1, district = -1, ward = -1;
            int.TryParse(ddlCity.SelectedValue, out city);
            int.TryParse(ddlDistrict.SelectedValue, out district);
            int.TryParse(ddlWard.SelectedValue, out ward);

            var data = service.GetOrderByConditions((DateTime)rdFromDate.SelectedDate, (DateTime)rdToDate.SelectedDate, txtKey.Text, txtKey.Text, txtKey.Text, txtKey.Text, Int32.Parse(ddlStatus.SelectedValue), city, district, ward);

            Session["ExportData"] = data;

            //Export file excell
            //ExcelAdo.GenerateReport(MapPath("~/OrderReport.xls"), data);

            //Download excell file
            //Response.Redirect("OrderReport.xls");

            //Redirect to report page
            Response.Redirect("Report/ReportPage.aspx");
        }

        protected void RadGrid_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
        {
            _pageIndex = e.NewPageIndex + 1;
        }

        protected void RadGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            try
            {
                if ((e.Item is GridEditableItem && e.Item.IsInEditMode))//for remove duplicate caption and edit form template
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    try
                    {
                        item.Controls[1].Controls[0].Controls[1].Controls[0].Controls[0].Visible = false;
                    }
                    catch
                    {
                    }
                }

                if (e.Item is GridDataItem)//for item template
                {
                    if (e.Item is GridDataItem)//for item template
                    {
                        Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                        if (lblSTT != null)
                            lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                    }
                    Image imgDetail = (Image)e.Item.FindControl("imgDetail");
                    //Image imgUpdate = (Image)e.Item.FindControl("imgUpdate");
                    //Image imgDelete = (Image)e.Item.FindControl("imgDelete");

                    string Id = ((BSPortal.Order)e.Item.DataItem).OrderId.ToString();
                    string script = string.Format("document.getElementById('{0}').value='{1}'; return true;", hdId.ClientID, Id);
                    //string script2 = string.Format("document.getElementById('{0}').value='{1}'; if(confirm('Xóa đơn đặt hàng này ?')) return true; else return false;", hdId.ClientID, Id);
                    imgDetail.Attributes.Add("onclick", script);
                    //imgUpdate.Attributes.Add("onclick", script);
                    //imgDelete.Attributes.Add("onclick", script2);
                }
            }
            catch (Exception)
            {
            }
        }

        protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            BindData();
        }

        protected void imgAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            RadWindow frmProductPriceNewsWindow = frmProductPriceNews;
            frmProductPriceNews.Height = 650;
            frmProductPriceNews.Title = "Thêm Mới";
            frmProductPriceNewsWindow.NavigateUrl = "frmListBook.aspx";
            frmProductPriceNewsWindow.Visible = true;
            frmProductPriceNewsWindow.Width = 800;
            frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }

        protected void imgDetail_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            RadWindow frmProductPriceNewsWindow = frmProductPriceNews;
            frmProductPriceNews.Height = 650;
            frmProductPriceNewsWindow.Title = "Chi tiết";
            frmProductPriceNewsWindow.NavigateUrl = "frmOrderDetail.aspx?OrderId=" + hdId.Value;
            frmProductPriceNewsWindow.Visible = true;
            frmProductPriceNewsWindow.Width = 800;
            frmProductPriceNewsWindow.KeepInScreenBounds = false;
            frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }

        protected void imgUpdate_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            RadWindow frmProductPriceNewsWindow = frmProductPriceNews;
            frmProductPriceNews.Height = 650;
            frmProductPriceNewsWindow.Title = "Chỉnh sửa thông tin đơn đặt hàng";
            frmProductPriceNewsWindow.NavigateUrl = "frmOrder.aspx?OrderId=" + hdId.Value;
            frmProductPriceNewsWindow.Visible = true;
            frmProductPriceNewsWindow.Width = 800;
            frmProductPriceNewsWindow.KeepInScreenBounds = false;
            frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }

        protected void imgDelete_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            MainService service = Common.GetClientService();
            int retcode = service.DeleteOrder(short.Parse(hdId.Value));
            if (retcode == (int)Common.RetCode.SUCESS)
            {
                BindData();
            }
        }
        #endregion

        #region Click
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            RadGrid.CurrentPageIndex = 0;
            _pageIndex = 1;

            RadGrid.DataSource = new object[0];
            RadGrid.VirtualItemCount = 0;
            RadGrid.DataBind();

            BindData();
        }
        #endregion    

        public class CategorySearch
        {
            private int _Id;

            public int Id
            {
                get { return _Id; }
                set { _Id = value; }
            }
            private string _Name;

            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }
            public CategorySearch() { }
        }
    }
}